
from typing import Final , Optional
import json


class Position:
    """ 机位 """

    def __init__(self , camera_id , device_no , camera_id_remote , camera_name, capture_region_infos):
        self.camera_id = camera_id
        self.device_no = device_no
        self.camera_id_remote = camera_id_remote
        self.camera_name = camera_name
        self.capture_region_infos = capture_region_infos
        pass


    def __str__(self):
        # return f'({self.timestamp}, {str(self.sleeves)})'
        return f'camera_id:{self.camera_id}, device_no: {self.device_no} ,camera_id_remote: {str(self.camera_id_remote)} , camera_name:{self.camera_name} , capture_region_infos:{self.capture_region_infos}'

    __repr__ = __str__


def peel_position(logger,position_data):
    """
    解析机位数据
    {'machineLocationRelId': 53, 'factoryId': 1, 'proLineId': 1, 'machineLocationId': 22, 'qualitylocationId': 1, 'qualitylocationName': '身袖左前', 'deviceInfoId': 75, 'bindingStatus': 1, 'bindingTime': '2023-08-03', 'deviceType': None, 'deviceNo': '148CCC50C047735AB66D7081F5F4F19F09E820AB', 'proLineCode': None, 'proLineName': None, 'configJson': '{"light":false, "focus":"2.87456748" , "exposure":"3000000", "adjust_delay":"2400", "waist_delay":"2500,1500"}', 'errorPointJsonList': '{"89":"{\\"matrix\\":\\"[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]\\"}","90":"{\\"matrix\\":\\"[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]\\"}","85":"{\\"matrix\\":\\"[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]\\"}"}', 'errorJSONObjectList': None, 'deviceConfig': None}
    """
    positions = []
    try:
        for item in position_data:
            camera_id = item["qualitylocationId"] if "qualitylocationId" in item else ""
            camera_name = item["qualitylocationName"] if "qualitylocationName" in item else ""
            device_no = item["deviceNo"] if "deviceNo" in item else ""
            qualitylocation_id = item["qualitylocationId"] if "qualitylocationId" in item else ""


            capture_region_infos = list()
            if "configJson" in item:
                config_json = item["configJson"]
                regions = json.loads(config_json)
                for region in regions:
                    capture_region = region["captureRegion"]
                    capture_region_id = capture_region["captureRegionId"]
                    capture_region_name = capture_region["captureRegionName"]
                    capture_camera_params = capture_region["config"]

                    defectives = region["defectiveProductss"]
                    defective_infos = list()
                    for defective in defectives:
                        defective_code = defective["defectiveProductsCode"]
                        defective_name = defective["defectiveProductsName"]
                        defective_rel_ids = defective["defectiveProductsDefectiveRelIds"]
                        defective_rel_names = defective["defectiveProductsDefectiveRelNames"]
                        defective_matrix = defective["config"]
                        defective_infos.append(dict(defective_code=defective_code,
                                                    defective_name=defective_name,
                                                    defective_rel_ids=defective_rel_ids,
                                                    defective_rel_names=defective_rel_names,
                                                    defective_matrix=defective_matrix))

                    capture_region_info = dict(capture_region_id=capture_region_id,
                                               capture_region_name=capture_region_name,
                                               capture_camera_params=capture_camera_params,
                                               defective_infos=defective_infos)
                    capture_region_infos.append(capture_region_info)


            p = Position(camera_id, device_no, qualitylocation_id, camera_name, capture_region_infos)
            positions.append(p)
    except Exception as e:
        logger.info(f"----peel_position----error:{e}")
        pass
    return positions
